﻿using CuaHangLego.Bussiness;
using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using CuaHangLego.Webform.App_Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CuaHangLego.Webform
{
    public partial class Account : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserAccess.IsLogin())
            {
                IList<HoaDonBan> hdbs = HoaDonBanDA.Instance.GetAllByKhachHangID(UserAccess.currentUser.MaSo);
                hdbs = hdbs.OrderByDescending(hd => hd.ThoiGian).Take(10).ToList();
                UserInfoView.DataSource = new object[] { UserAccess.currentUser };
                UserInfoView.DataBind();
                txtSoHD.Text = hdbs.Count.ToString();
                txtSoHDGiao.Text = hdbs.Where(hd => hd.DaThanhToan == true).Count().ToString();
                txtSoHDChuaGiao.Text = hdbs.Where(hd => hd.DaThanhToan == false).Count().ToString();
                int temp = 0;
                int temp2 = 0;
                foreach (HoaDon hd in hdbs)
                {
                    var t = from ct in hd.DsCTHD
                            select ct.SanPham.GiaBan * ct.SoLuong;
                    foreach (int value in t)
                        temp += value;
                    var t2 = from ct in hd.DsCTHD
                             select ct.SoLuong;
                    foreach (int value in t2)
                        temp2 += value;
                }
                txtSoSP.Text = temp2.ToString();
                txtTongTien.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", temp);
                foreach (var hd in hdbs)
                {
                    temp = 0;
                    var t = from ct in hd.DsCTHD
                            select ct.SanPham.GiaBan * ct.SoLuong;
                    foreach (int value in t)
                        temp += value;
                    TreeNode node = new TreeNode("#" + hd.MaSo + " - " + hd.ThoiGian.ToString() 
                        + "<span style='float:right;color:#333;'><b>" 
                        + String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", temp) 
                        + "</b></span>");
                    foreach(CTHD ct in hd.DsCTHD)
                    {
                        node.ChildNodes.Add(
                            new TreeNode("<span style='float:left; color:#444;'>" + ct.SanPham.Ten + "</span>" 
                                + "<span style='float:right; color:#444;'>" 
                                + ct.SoLuong 
                                + " x " 
                                + String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", ct.SanPham.GiaBan) 
                                + " = " 
                                + String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", ct.SoLuong * ct.SanPham.GiaBan) 
                                + "</span>") { NavigateUrl = "javascript:void(0)" });
                        node.NavigateUrl = "javascript:void(0)";
                    }
                    listOrder.Nodes.Add(node);
                }
                listOrder.CollapseAll();
            }
        }
    }
}